package com.entrogy.dao;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface ClassMapper {
    @Insert("insert into class_information (id,class_name ,parent_id,school_code) values (#{id},#{className}, #{parentId},#{schoolCode})")
	int insertGrade(@Param("id") int id,@Param("className") String className, @Param("parentId") int parentId,@Param("schoolCode") int schoolCode);

    @Insert("insert into user_class (u_id,c_id) values (#{userId},#{classId})")
	int insertUserAndClass(@Param("userId") int userId,@Param("classId") int classId);
	@Results({
			@Result(property="classTypeId",column="class_type_id"),
			//classTypeEntity映射ClassTypeEntity classTypeEntity，many=@Many是调用关联查询方法，"id"是关联查询条件，FetchType.LAZY是延迟加载
			@Result(property="classTypeEntity",column="class_type_id", one=@One(select="com.entrogy.dao.ClassMapper.getClassTypeById",fetchType= FetchType.LAZY))
	})
    @Select("<script>"
    		+"select * from class_information where school_code = #{schoolCode} "
			+" "
			+ "<if test='type==1'>"
			+ " and parent_id!=1"
			+ "</if> "
			+" "
			+ "<if test='type==2'>"
			+ " and parent_id=#{parentId}"
			+ "</if> "
			+" "
			+ "<if test='classType != null and classType != \"\" '>"
			+ " and class_type_id=#{classType} "
			+ "</if> "
			+" "
			+ "<if test='className != null and className != \"\" '>"
			+ " and class_name LIKE \'%${className}%\' "
			+ "</if> "
			+" "
			+ " order by parent_id,id"
			+" "
			+"<if test='page != null and total != null '>"
			+"LIMIT #{page},#{total}"
			+"</if>"
			+"</script>")
	List<ClassEntity> getClassList(Map<String,Object> map);

    @Delete("delete from class_information where id = #{id} and school_code=#{schoolCode}")
	int deleteClass(@Param(value = "id") int id, @Param(value = "schoolCode") Integer schoolCode);

    @Insert("insert into class_type(type_name,school_code) values(#{typeName},#{schoolCode})")
	int insertClassType(@Param("typeName") String typeName,@Param("schoolCode")int schoolCode);

    @Select("select * from class_type where school_code = #{schoolCode}")
	List<ClassTypeEntity> getClassTypeList(int schoolCode);

	@Select("select * from class_type where id = #{id}")
	ClassTypeEntity getClassTypeById(int id);

    @Update("update class_information  set class_type_id = #{classTypeId},class_name = #{className} where id = #{classId} and school_code=#{schoolCode}")
	int updateClassInformation(@Param("classId") int classId, @Param("classTypeId") int classTypeId, @Param("className") String className, @Param(value = "schoolCode") Integer schoolCode);

	@Results({
			@Result(property="classTypeId",column="class_type_id"),
			//classTypeEntity映射ClassTypeEntity classTypeEntity，many=@Many是调用关联查询方法，"id"是关联查询条件，FetchType.LAZY是延迟加载
			@Result(property="classTypeEntity",column="class_type_id", one=@One(select="com.entrogy.dao.ClassMapper.getClassTypeById",fetchType= FetchType.LAZY))
	})
    @Select("select * from class_information  where school_code = #{schoolCode} and parent_id!=1 order by parent_id desc")
	List<ClassEntity> getClasses(@Param("schoolCode") int schoolCode);

	@Delete("delete from class_type where id = #{id}")
	int deleteClassType(int id);

	/**
	 *   根据id 查询class
	 * @param id
	 * @return
	 */
	@Select("select * from class_information where id = #{id} and school_code=#{schoolCode}")
	ClassEntity getClassById(@Param("id") int id, @Param("schoolCode") int schoolCode);

	@Select("<script>"
			+"select count(id) from class_information where school_code = #{schoolCode}"
			+" "
			+ "<if test='type==1'>"
			+ " and parent_id!=1"
			+ "</if> "
			+" "
			+ "<if test='type==2'>"
			+ " and parent_id=#{parentId}"
			+ "</if> "
			+" "
			+ "<if test='classType!=null and classType != \"\" '>"
			+ " and class_type_id=#{classType} "
			+ "</if> "
			+" "
			+ "<if test='className!=null and className != \"\" '>"
			+ " and class_name LIKE \'%${className}%\' "
			+ "</if> "
			+"</script>")
	int getClassListTotal(Map<String,Object> map);


	/**
	 * 通过班级id查询年级id
	 * @param classId
	 * @param schoolCode
     * @return
	 */
	@Select("<script>"
			+"select parent_id from class_information where id = #{classId} and school_code=#{schoolCode}"
			+"</script>")
	int getGradeIdByClassId(@Param(value = "classId") int classId, @Param(value = "schoolCode") Integer schoolCode);

	/**
	 * 根据学生的thdid和班级ID查询出其班级名称
	 * @param classId
	 * @param personId
	 * @return
	 */
	@Select("select class_name from class_information c,student_information s where c.school_code = s.school_code and c.id = #{courseId} and s.student_thdid = #{personId}")
	String getClassName(@Param("classId") String classId,@Param("personId") String personId);

	@Select("select class_name from class_information where id = #{classId} and school_code = #{schoolCode}")
	String getClassNameById(@Param("classId") String classId, @Param("schoolCode") int schoolCode);

	@Select("select id,class_name from class_information where school_code = #{schoolCode} and parent_id = 1")
	List<ClassEntity> getGradeList(@Param("schoolCode") int schoolCode);

	/**
	 * 根据年级ID查询出其所对应的班级列表
	 * @param id
	 * @return
	 */
	@Select("select * from class_information where parent_id = #{id} and school_code = #{schoolCode}")
	List<ClassEntity> getClassListByGradeId(@Param("id") int id,@Param("schoolCode") int schoolCode);

	/**
	 * 根据班主任id查询所教班级信息
	 * @param uid
	 * @return
	 */
	@Select("select id,class_name from class_information where id = (select class_id from `user` where id=#{uid}) and school_code=(select school_code from `user` where id=#{uid})")
	ClassEntity getClassByClassLeaderId(String uid);

	/**
	 * 根据年级主任的id查询所带年级的信息，包括班级
	 * @param uid
	 * @return
	 */
	GradeEntity getGradeByGradeLeaderId(String uid);

	/**
	 * 根据学校id查询该校的所有年级id
	 * @param schoolCode
	 * @return
	 */
	List<Integer> listGradeIdBySchoolCode(Integer schoolCode);


	/**
	 * 查询班主任所带的班级id
	 * @param teacherId
	 * @return
	 */
	List<Integer> listClassLeaderManagementClassId(String teacherId);

	/**
	 * 查询年级主任所管的班级id
	 * @param teacherId
	 * @return
	 */
	List<Integer> listGradeLeaderManagementClassId(String teacherId);

	/**
	 * 删除年级时删除早上的考勤时间
	 * @return
	 */
    int deleteGradeMorningAttendanceTime(@Param(value = "gradeId") int gradeId, @Param(value = "schoolCode") Integer schoolCode);

	/**
	 * 该班级是否有班主任
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @return
	 */
    String getClassLeader(@Param(value = "schoolCode") int schoolCode, @Param(value = "gradeId") String gradeId, @Param(value = "classId") String classId);

	/**
	 * 插入年级静态时间规则表
	 * @param id
	 * @param outType
	 * @param cycleId
	 * @param cycleName
	 * @param type
	 * @param timeSlot
	 * @param schoolCode
	 * @return
	 */
	int insertGradeStaticCycle(@Param("id") int id, @Param("outType") String outType, @Param("cycleId") Integer cycleId, @Param("cycleName") String cycleName, @Param("type") Integer type, @Param("timeSlot") String timeSlot, @Param("schoolCode") int schoolCode);

	/**
	 * 初始化年级的静态时间段表
	 * @param staticArrangeList
	 * @return
	 */
	int insertStaticArrange(@Param(value = "staticArrangeList") List<StaticArrangeEntity> staticArrangeList);

	/**
	 * 删除年级的静态时间安排表
	 * @param id
	 * @param schoolCode
	 * @return
	 */
	int deleteGradeStaticArrange(@Param("id") int id, @Param("schoolCode") Integer schoolCode);

	/**
	 * 删除年级的静态时间规则表
	 * @param id
	 * @param schoolCode
	 * @return
	 */
	int deleteGradeStaticCycle(@Param("id") int id, @Param("schoolCode") Integer schoolCode);

	/**
	 * 查询该年级在实义德后台的时间规则的id
	 * @param courseId
	 * @param schoolCode
	 * @return
	 */
	List<Integer> listGradeCycle(@Param("courseId") int courseId, @Param("schoolCode") Integer schoolCode);

    StaticCycleEntity getStaticCycle(@Param("gradeId") int gradeId, @Param("schoolCode") Integer schoolCode, @Param("allowTime") String allowTime);

	/**
	 * 查询年级组长id
	 * @param schoolCode
	 * @param gradeId
	 * @return
	 */
    String getGradeLeaderId(@Param("schoolCode") Integer schoolCode, @Param("gradeId") Integer gradeId);

	/**
	 * 查询班级/年级id
	 * @param schoolCode
	 * @param gradeName
	 * @return
	 */
	String getClassId(@Param("schoolCode") Integer schoolCode, @Param("gradeName") String gradeName);

	/**
	 * 查询学校的所有班级id
	 * @param schoolCode
	 * @param gradeId
	 * @return
	 */
    List<Integer> listClassIdBySchoolCode(@Param("schoolCode") Integer schoolCode, @Param("gradeId") Integer gradeId);

	/**
	 * 查询该学校该时段的最早考勤时间和最晚考勤时间
	 * @param schoolCode
	 * @param dynArrangeTableName
	 * @return
	 */
    List<JSONObject> listAttendanceTime(@Param("schoolCode") Integer schoolCode, @Param("dynArrangeTableName") String dynArrangeTableName);
}
